package com.gantang.poc.task;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.Callable;

@Slf4j
public class ApiTask implements Callable<ResponseEntity<String>> {
    private final RestTemplate restTemplate;
    private final String url;
    private final Object requestBody;

    public ApiTask(RestTemplate restTemplate, String url, Object requestBody) {
        this.restTemplate = restTemplate;
        this.url = url;
        this.requestBody = requestBody;
    }

    @Override
    public ResponseEntity<String> call() throws Exception {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Object> requestEntity = new HttpEntity<>(requestBody, headers);
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
        log.info("Task executed with response: " + response.getBody());
        return response;
    }
}
